﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace NJL.WebUsers
{
    public partial class CompanyOrder : BasePageVisitor
    {
        private readonly BLL.NJL_COMPANY bllCompany = new BLL.NJL_COMPANY();
        private readonly BLL.NJL_BOOK bllBook = new BLL.NJL_BOOK();
        private readonly BLL.NJL_BOOK_ORDER bllBookOrder = new BLL.NJL_BOOK_ORDER();

        protected Model.NJL_BOOK MODEL_BOOK;
        protected Model.NJL_COMPANY MODEL_COMPANY;
        NJL.BLL.NJL_USER bllUser = new BLL.NJL_USER();
        NJL.Model.NJL_USER modelUser;

        NJL.BLL.NJL_PERSON bllPerson = new BLL.NJL_PERSON();
        NJL.Model.NJL_PERSON modelPerson;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (UserId == "" || UserName == "")
                {
                    Response.Redirect("/Login.aspx?url=" + Server.UrlEncode(Request.RawUrl), true);
                }
                if (UserId != null)
                {
                    modelUser = bllUser.GetModel(UserId);
                    {

                        txtBookEmail.Text = modelUser.EMAIL;
                        txtPickEmail.Text = modelUser.EMAIL;
                        txtPickMobile.Text = modelUser.MOBILE;
                        txtBookMobile.Text = modelUser.MOBILE;
                    }
                    DataSet ds = bllPerson.GetList("NJL_USER_ID='" + UserId + "'");
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        modelPerson = bllPerson.GetModel(ds.Tables[0].Rows[0][0].ToString());
                        txtPickName.Text = modelPerson.REAL_NAME;
                        txtBookName.Text = modelPerson.REAL_NAME;
                    }
                }
                LoadData();
            }
        }
        private void LoadData()
        {
            MODEL_BOOK = bllBook.GetModel(Request.QueryString["bid"]);
            MODEL_COMPANY = bllCompany.GetModel(Request.QueryString["id"]);
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            LoadData();
            //
            string strTripDate = txtTripDate.Text.Trim();
            string strTicketCount = txtTicketCount.Text.Trim();
            string strPickName = txtPickName.Text.Trim();
            string strPickMobile = txtPickMobile.Text.Trim();
            string strPickEmail = txtPickEmail.Text.Trim();
            string strBookName = txtBookName.Text.Trim();
            string strBookMobile = txtBookMobile.Text.Trim();
            string strBookEmail = txtBookEmail.Text.Trim();
            string strOrderTotal = hidTotalPrice.Value;

            Model.NJL_BOOK_ORDER model = new Model.NJL_BOOK_ORDER();
            model.ID = System.DateTime.Now.ToString("yyyyMMddHHmmssfff") + new Random().Next(100, 999).ToString();
            model.CREATE_DATE_TIME = System.DateTime.Now;
            model.CREATE_USER_ID = UserId;
            model.CREATE_USER_NAME = UserName;
            model.NJL_BOOK_ID = Request.QueryString["bid"];
            model.PICK_NAME = strPickName;
            model.PICK_MOBILE = strPickMobile;
            model.PICK_EMAIL = strPickEmail;
            model.BOOK_NAME = strBookName;
            model.BOOK_MOBILE = strBookMobile;
            model.BOOK_EMAIL = strBookEmail;
            model.TYPE_CODE = MODEL_BOOK.NJL_CS_BOOK_PAY_TYPE_CODE;
            model.VISIT_DATE = Convert.ToDateTime(txtTripDate.Text.Trim());
            model.NJL_SYS_BASE_ID = WEBSITE_CODE;
            model.ORDER_TOTAL = Convert.ToDecimal(strOrderTotal);
            model.TICKET_COUNT = Convert.ToInt32(strTicketCount);
            model.ORDER_STATE = 0;//创建
            MODEL_BOOK = bllBook.GetModel(Request.QueryString["bid"]);
            if (MODEL_BOOK.NJL_CS_BOOK_PAY_TYPE_CODE == "2")//景点现付
            {
                model.ORDER_STATE = 1;                      //订单完成
            }
            if (MODEL_BOOK.NJL_CS_BOOK_PAY_TYPE_CODE == "1")//在线支付
            {
                model.ORDER_STATE = 2;                      //待支付状态
            }
            bllBookOrder.Add(model);
            if (model.ORDER_STATE == 2)
            {
                Response.Redirect("/CompanyOrderPay.aspx?id=" + model.ID, true);
            }
            if (model.ORDER_STATE == 1)
            {
                Response.Redirect("/CompanyOrderOK.aspx?id=" + model.ID, true);
            }
        }
    }
}